import { useRoutes, useLocation,useNavigate} from 'react-router-dom'
import { lazy, Suspense,useEffect } from 'react'


const Login = lazy(() => import('../pages/Login'))


const routes = [
   
    {
        path: '/login',
        element: <Login />
    },

]



function RouterList() {
    let element = useRoutes(routes)  // 读取路由数组
    return element
}

function Router() {
    const location = useLocation();
    const navigate = useNavigate()
    useEffect(() => {
    //   console.log('路由变化',location.pathname);
      if (location.pathname != '/login') {
       const token = JSON.parse(localStorage.getItem('token')) 
       if (!token || !token?.phone) {
        navigate('/login')
       }
      }
    }, [location]);
    return (
        // 加载菊花图
        <Suspense>
            <RouterList />
        </Suspense>
    )
}

export default Router